package com.masterclient.app.modules.purchase.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselibrary.bean.goods.ProductSpecificationData;
import com.eoner.baselibrary.bean.goods.SimpleProductDetailData;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;


/**
 * @auther hjian
 * @date 2020/9/22.
 */
public class PurchaseProductDetailPresenter extends PurchaseProductDetailContract.Presenter {

    @Override
    public void getData(String product_id) {
        toSubscribe(RetrofitUtil2.getInstance().simpleProductDetail(product_id), new Consumer<CommonBaseResponse<SimpleProductDetailData>>() {
            @Override
            public void accept(CommonBaseResponse<SimpleProductDetailData> bean) {
                if (bean.getCode() == 0){
                    mView.showData(bean.getData());
                }else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        });
    }

    @Override
    public void getProductSpecification(String product_id) {
        toSubscribe(RetrofitUtil2.getInstance().productSpecification(product_id), new Consumer<CommonBaseResponse<ProductSpecificationData>>() {
            @Override
            public void accept(CommonBaseResponse<ProductSpecificationData> bean) {
                if (bean.getCode() == 0){
                    mView.showSpecificationData(bean.getData());
                }else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        });
    }

    @Override
    public void addCart(String product_id, String num) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id",product_id);
            jsonObject.put("num",num);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().purchaseCartAdd(body), new Consumer<CommonBaseResponse<Map<String,String>>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<Map<String,String>> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.successAddCart(bean.getData());
                }else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        }));
    }

    @Override
    public void confirmOrder(String buy_product, String buy_type, String is_custody) {

        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject buyProduct = new JSONObject(buy_product);
            jsonObject.put("buy_product",buyProduct);
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type","4");
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if ("0".equals(bean.getCode())) {
                    mView.confirmOrderSuccess(bean.getData(),is_custody);
                } else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}
